﻿using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {


    // Control variables
    public bool facingRight = true;
    private bool jump = false;
    private bool secondJump = false;
    public float maxSpeed = 5f;
    public float jumpForce = 1000f;
    public float secondJumpForce = 0f;
    private int numJumps = 0;

    // Ground Check variables
    private Transform groundCheck;
    public bool grounded = false;
    public LayerMask whatIsGround;

    // Animation variables
    private Animator anim;

    // ID variables
    public bool isEnemy = true;

    void Awake()
    {
        groundCheck = transform.Find("groundCheck");
        anim = GetComponent<Animator>();
    }

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        grounded = Physics2D.Linecast(transform.position, groundCheck.position, whatIsGround);

        
        // TODO: Cleanup this code
        if(Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.W)) {
            if (grounded) {
                jump = true;
                numJumps = 1;
            } else if(numJumps == 1) {
                secondJump = true;
                numJumps = 2;
            }
        }
        if(numJumps == 2) {
            numJumps = 0;
        }
	}

    void FixedUpdate()
    {
        // Cache the horizontal input.
        float h = Input.GetAxis("Horizontal");

        // The Speed animator parameter is set to the absolute value of the horizontal input.
        anim.SetFloat("Speed", Mathf.Abs(h));

        // The vSpeed animator parameter is set to the objects vertical speed
        anim.SetFloat("vSpeed", Mathf.Abs(rigidbody2D.velocity.y));

        rigidbody2D.velocity = new Vector2(h * maxSpeed, rigidbody2D.velocity.y);

        // If the input is moving the player right and the player is facing left...
        if (h > 0 && !facingRight)
            // ... flip the player.
            Flip();
        // Otherwise if the input is moving the player left and the player is facing right...
        else if (h < 0 && facingRight)
            // ... flip the player.
            Flip();

        // If the player should jump...
        if (jump)
        {
            // Add a vertical force to the player.
            rigidbody2D.AddForce(new Vector2(0f, jumpForce));

            // Make sure the player can't jump again until the jump conditions from Update are satisfied.
            jump = false;
        }
        if (secondJump) {
            rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0f);
            rigidbody2D.AddForce(new Vector2(0f, secondJumpForce));

            secondJump = false;
        }
    }

    void Flip()
    {
        // Switch the way the player is labelled as facing.
        facingRight = !facingRight;

        // Multiply the player's x local scale by -1.
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
}
